using System;
using System.Collections.Generic;
using NUnit.Framework;

namespace VisitorPattern
{
   public delegate void VisitorAction<T>(T s);

   public static class MyVisitAllExtensionMethod
   {
      public static void VisitAll<T>(this IEnumerable<T> source, VisitorAction<T> action)
      {
         foreach (var s in source) action(s);
      }
      
      public static int GetLength(this string[] source)
      {
         Console.WriteLine("Hello");
         return source.GetLength(0);
      }
   }

   [TestFixture]
   public class When_asked_to_visit_all_of_the_elements_in_a_collection
   {
      [Test]
      public void Should_visit_each_element_and_call_the_passed_delegate()
      {
         var strings = new[] {"ONE", "TWO", "THREE"};

         int count = 0;

         strings.VisitAll(str =>
                             {
                                Assert.IsTrue(str == str.ToUpper());
                                count++;
                             });

         Assert.AreEqual(3, count);
      }

      [Test]
      public void Trying_to_overwrite_an_existing_method()
      {
         var strings = new[] { "ONE", "TWO", "THREE" };

         Assert.AreEqual(3, strings.GetLength());
      }
   }
}